<?php

namespace app\admin\controller\financing;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 周边产品收费记录-场所
 *
 * @icon fa fa-user
 */
class Product extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\Registration;
        $this->model = new \app\admin\model\Orders;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                //->with(["products"=>['id','name','type']])
                ->with([
                    'products' => function ($query) {
                        $query->withField('id,name,type');
                    }
                ])
                ->where($where)
                ->where('status','in',[1,2])
                ->where('orders.type',2)
                ->order('id', 'desc')
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){

                    $list->items()[$k]['money'] = $v->money/100;


                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 到场操作
     * @param $ids
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\DbException
     *
     */
    public function for($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isAjax()) {

            $update = Db::table('fa_orders')
                ->where('id',$ids)
                ->update([
                    'status' => 2,
                    'update_time' => time(),
                ]);

            if ($update !== false){
                $this->success("成功", null, ['id' => $ids]);
            }else{
                $this->error(__('失败'));
            }


        }
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }






}
